#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN (100)
void inputString(const char * prompt, char * input)
{
        input[0] = 0;
        printf("%s", prompt);
        scanf("%[^\n]", input);
        fflush(stdin);
}
void crypt(char * line, char * line2, char * key)
{
	FILE * inFile = NULL, * outFile = NULL;
	int i, s, r;
	inFile = fopen(line, "rb");
	outFile = fopen(line2, "wb");
	if((inFile == NULL)||(outFile == NULL))
	{
		printf("Error with file(s) opening has been occured!\n");
		system("PAUSE");
		fclose(inFile);
		fclose(outFile);
		return;
	}
	for(i=0; (r=fgetc(inFile))!=EOF; i++)
	{
		s = r ^ key[i%strlen(key)];
		fputc(s, outFile);
	}
	fclose(inFile);
	fclose(outFile);
}
void main(int argc, char * argv[])
{
	char line[MAX_LEN]="", line2[MAX_LEN]="", key[MAX_LEN]="";
	int i;
	if(argc==1)
	{
		inputString("Enter input file name: ", line);
		inputString("Enter output file name: ", line2);
		inputString("Enter key: ", key);
		crypt(line, line2, key);
	}
	if(argc==2)
	{
		inputString("Enter input file name: ", line);
		inputString("Enter output file name: ", line2);
		crypt(line, line2, argv[1]);
	}
	if(argc==3)
	{
		inputString("Enter output file name: ", line2);
		crypt(argv[2], line2, argv[1]);
	}
	if((argc==4)||(argc>4))
	{
		crypt(argv[2], argv[3], argv[1]);
	}
}